//
//  LS_SearchTextfield.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/9.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_SearchTextfield.h"
@interface LS_SearchTextfield()<UITextFieldDelegate>

/**
 搜索小图标
 */
@property(nonatomic,strong)UIImageView *imageView;

/**
 搜索框
 */
@property(nonatomic,strong)UITextField *textfield;
@end

@implementation LS_SearchTextfield

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        [self setConfig];//基本配置h h
        self.backgroundColor = LS_SetColor(235, 235, 235);
        self.layer.cornerRadius = 15;
        self.layer.masksToBounds = YES;
        [self addSubview:self.imageView];
        [self addSubview:self.textfield];
        
       
    }return self;
}
-(void)setConfig{
    self.isEdit = YES;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
        make.height.width.offset(15);
    }];
    
    [self.textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(10);
        make.centerY.offset(0);
        make.right.offset(-10);
        make.height.offset(30);
    }];
    
}
-(UIImageView *)imageView
{
    return LS_LAZY(_imageView, ({
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = LS_Getimage(@"home_top_search");
        imageView.backgroundColor = [UIColor clearColor];
        imageView;
    }));
}
-(UITextField *)textfield
{
    return LS_LAZY(_textfield, ({
        UITextField *textfield = [[UITextField alloc]init];
        textfield.placeholder = @"请输入搜索内容";
        textfield.layer.cornerRadius = 15;
        textfield.layer.masksToBounds = YES;
        textfield.delegate = self;
        textfield.textColor = [UIColor blackColor];
        textfield.font = LS_FontWithname(@"STHeitiK-Light", 13);
        textfield.backgroundColor = [UIColor clearColor];
        [textfield addTarget:self action:@selector(textfield:) forControlEvents:UIControlEventValueChanged];
        textfield;
    }));
}
-(void)textfield:(UITextField *)textfield
{
    
    if (self.textfieldReturn) {
        self.textfieldReturn(textfield.text);
    }
    
}
-(void)setIconImage:(NSString *)iconImage
{
    _iconImage = iconImage;
    self.imageView.image = LS_Getimage(iconImage);
}
-(void)setTextfieldColor:(UIColor *)textfieldColor
{
    _textfieldColor = textfieldColor;
    self.textfield.textColor = textfieldColor;
}
-(void)setPlaceHoldColor:(UIColor *)placeHoldColor
{
    _placeHoldColor = placeHoldColor;
    [self setValue:placeHoldColor forKeyPath:@"_placeholderLabel.textColor"];
   
}
-(void)setPlaceHold:(NSString *)placeHold
{
    _placeHold = placeHold;
    self.textfield.placeholder = placeHold;
}
-(void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    //不能编辑 点击整个可以走这个方法
    if (isEdit) {
        self.textfield.enabled = YES;
    }else{
        self.textfield.enabled = NO;
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
        [self addGestureRecognizer:tap];
    }
}
-(void)tapClick
{
    if (self.tapSearchClick) {
        self.tapSearchClick();
    }
}

@end
